
import scrollOpt from '../base/scrollOptimize'
import { WX_AD_CONFIG, Sdk_Platform } from '../../frame/constants';
import { UI_Layer, UI_Listen } from '../../config/ui_def'
cc.Class({
    extends: scrollOpt,

    properties: {
        touchNode: cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {

    },

    update(dt) {
        if (this.canAni) {
            this.scrollTime += dt
            if (this.scrollTime >= this.timeToScroll) {
                this.timeToScroll = this.randomNum(2, 3)
                this.aniMation()
                this.scrollTime = 0
            }
        }

    },



    initPage() {
        if (!he.pool.getPool('fakeRankItem')) {
            he.pool.createPoolSync('fakeRankItem', 5, cc.Prefab, 'uimod/fakeRankItem')
        }

    },

    updatePage() {
        this.scrollInit()
        this.scroll.scrollToTop()
        this.adSwitch(true)
        this.openListen()
        this.canAni = true
        this.scrollTime = 0
        this.timeToScroll = this.randomNum(2, 3)
        this.scrollIndex = 0
        this.maxScrollOffset = 1000
        this.scrollClick = false
        this.touchNode.on(cc.Node.EventType.TOUCH_START, this.scrollServer, this)
    },

    scrollServer() {
        if (!this.scrollClick) {
            this.scrollClick = true
            if (wx.tmSDK) {
                wx.tmSDK.flowNavigate({
                    positionId: this.posId, // 广告位id, 请先使用该id获取推广创意列表
                    creativeId: +this.crtId,  // 传入获取到的creativeId
                }).then((newList) => {
                    // console.log('跳转成功or取消跳转（可根据特有返回值区分两种情况）')
                    // console.log('自动刷新列表：', newList)//返回最新列表 
                    this.scrollClick = true
                    this.clickNode.off(cc.Node.EventType.TOUCH_START, this.scrollServer)
                }).catch((error) => {
                    console.log('跳转失败', error);
                    this.scrollClick = true
                })
            }
        }

    },


    closeUI() {
        for (let i = (this.content.children.length); i > 0; i--) {
            he.pool.putElement('fakeRankItem', this.content.children[i - 1])
        }
        this.node.targetOff(this)
        this.closeListen()
        this.canAni = false
    },

    adSwitch(status) {
        if (status) {
            switch (common.sdk_env) {
                case Sdk_Platform.WECHAT:
                    if (!common.showAd) return
                    he.ad.getTdAdJson(WX_AD_CONFIG.tmIcon[13], this.uiSet.bind(this))
                    break
            }
        } else {
            //  关掉广告
        }
    },

    uiSet(config) {
        if (config.isOpen) {
            for (let i = 0; i < config.creatives.length; i++) {
                let pre = he.pool.getElement('fakeRankItem')
                pre.position = cc.Vec2.ZERO
                pre.opacity = 255
                pre.parent = this.content
                let spt = pre.getComponent('fakeRankItem')
                spt.init(config.creatives[i], i)
                if (i == 0) {
                    this.posId = config.creatives[i].positionId
                    this.crtId = config.creatives[i].creativeId
                }
            }
        }
    },

    aniMation() {
        if (this.scrollIndex < 2) {
            this.scroll.scrollToOffset(cc.v2(0, this.maxScrollOffset / 6 * (this.scrollIndex + 1)), 0.2)
        }
        else if (this.scrollIndex >= 2 && this.scrollIndex < 4) {
            this.scroll.scrollToOffset(cc.v2(0, this.maxScrollOffset / 6 * (4 - this.scrollIndex - 1)), 0.2)
        }
        this.scrollIndex++
        if (this.scrollIndex == 4) {
            this.scrollIndex = 0
        }
    },


    returnServer() {
        // this.uiBase.closePage()
        gg.uiManager.addPage(UI_Layer.Load, {
            name: "LoadCover",
            in: true,
            scene: 'Main'
        })
    },

    //范围内的随机数
    randomNum(minNum, maxNum) {
        switch (arguments.length) {
            case 1:
                return parseInt(Math.random() * minNum + 1, 10);
                break;
            case 2:
                return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
                break;
            default:
                return 0;
                break;
        }
    },

});
